<?php


namespace app\admin\controller;


use app\common\controller\AdminController;
use think\Exception;
use think\facade\Request;

class AdminRole extends AdminController {

    public $resubmitLock = ['add', 'edit'];

    /**
     * 权限配置
     * 1查看权限、2管理权限、4导入权限、
     * @var array
     */
    protected $levelConfig = [
        1 => ['getlist', 'info', 'leveltree'],
        2 => ['add', 'edit', 'del'],
        4 => [],
        8 => []
    ];

    /**
     * 获取列表
     * @return false|string
     */
    public function getList() {
        try {
            $params = $this->checkParams('page,row');
            $adminRoleModel = new \app\admin\model\AdminRole();
            $listData = $adminRoleModel->getList($params);
            if (!$listData) return $this->resErr();
            return $this->resData('success', $listData);
        } catch (Exception $e) {
            return $this->resErr('操作失败~');
        }
    }

    /**
     * 角色添加
     * @return false|string
     * @throws \think\Exception
     */
    public function add() {
        try {
            $params = $this->checkParams('level,name,city_codes');
            $adminRoleModel = new \app\admin\model\AdminRole();
            if (!$adminRoleModel->add($params)) {
                return $this->resErr();
            }
            return $this->resData('添加成功', []);
        } catch (Exception $e) {
            return $this->resErr('操作失败~');
        }

    }

    /**
     * 角色修改
     * @return false|string
     * @throws \think\Exception
     */
    public function edit() {
        try {
            if (Request::isPost()) {
                $params = $this->checkParams('level,name,id,city_codes');
                $adminRoleModel = new \app\admin\model\AdminRole();
                if (!$adminRoleModel->edit($params)) {
                    return $this->resErr();
                }
                return $this->resData('修改成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr('操作失败~');
        }

    }

    /**
     * 获取详情
     * @return false|string
     */
    public function info() {
        try {
            $params = $this->checkParams('id');
            $adminRoleModel = new \app\admin\model\AdminRole();
            if (!$info = $adminRoleModel->info($params['id'])) {
                return $this->resErr('该数据不存在');
            }
            return $this->resData('success', $info);
        } catch (Exception $e) {
            return $this->resErr('操作失败~');
        }
    }

    /**
     * 删除
     * @return false|string
     */
    public function del() {
        try {
            $params = $this->checkParams('id');
            $adminRoleModel = new \app\admin\model\AdminRole();
            if (!$adminRoleModel->del($params['id'])) {
                return $this->resErr();
            }
            return $this->resData('删除成功', []);
        } catch (Exception $e) {
            return $this->resErr('操作失败~');
        }
    }

    /**
     * 权限树
     * @return false|string
     */
    public function levelTree() {
        return $this->resData('success', config('level.tree'));
    }
}